package com.hongdee.crm.carConfig.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.carConfig.service.ITCrmCarRefDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017/4/24.
 */
@RestController
@RequestMapping("/crm/config/vin")
public class TCrmCarRefDetailController {

    @Autowired
    private ITCrmCarRefDetailService detailService;


    @RequestMapping("/impPage")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "") String start,
                    @RequestParam(name = "limit", defaultValue = "") String limit
    ) throws Exception {

        Map map = detailService.impPage(uid, start, limit);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }


    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 导入确认
     * @author 徐钦勇
     */
    @RequestMapping("/impConfirm")
    @ResponseBody
    public ResultEntity impConfirm(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择记录！");
        }
        this.detailService.impConfirm(id, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("确认成功！");
        return res;
    }


    /**
     * @param data 批量数据
     * @return Map
     * @throws Exception
     * @desc 延保批量确认
     * @author 徐钦勇
     */
    @RequestMapping("/impConfirms")
    @ResponseBody
    public ResultEntity impConfirms(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestBody List<Map> data) throws Exception {
        detailService.multiImpConfirm(data, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("数据确认成功！");
        return res;
    }

    /**
     * @return Map
     * @throws Exception
     * @desc 延保全部确认
     * @author 徐钦勇
     */
    @RequestMapping("/impConfirmAll")
    @ResponseBody
    public ResultEntity impConfirmAll(@RequestParam(name = "uid", defaultValue = "") String uid,
                                      @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        detailService.impConfirmAll(uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("数据确认成功！");
        return res;
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 删除多条待导入的数据
     * @author 术洪超
     */
    @RequestMapping("/impDels")
    @ResponseBody
    public ResultEntity impDels(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestBody List<Map<Object, String>> data) throws Exception {
        if (org.springframework.util.StringUtils.isEmpty(data)) {
            throw new Exception("请选择记录！");
        }
        this.detailService.impDels(data, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 导入删除
     * @author 术洪超
     */
    @RequestMapping("/impDelAll")
    @ResponseBody
    public ResultEntity impDelAll(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        this.detailService.impDelAll(uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }

    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "saId", defaultValue = "") String saId,
                    @RequestParam(name = "distType", defaultValue = "") String distType,
                    //车牌号 1 车牌号 2 车架号 3尾号 4 尾数
                    @RequestParam(name = "allotType", defaultValue = "") String allotType,
                    //VIN
                    @RequestParam(name = "allotTypeV", defaultValue = "") String allotTypeV

    ) throws Exception {
        String oid = TokenUtil.getCompanyId(uid);
        Map map = this.detailService.query(oid, start, limit, saId, distType,allotType,allotTypeV);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }


    @RequestMapping("/distCar")
    @ResponseBody
    public Map distCar(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "saId", defaultValue = "") String saId,
                       @RequestParam(name = "type", defaultValue = "") String type,
                       @RequestParam(name = "value", defaultValue = "") String value) throws Exception {
        String oid = TokenUtil.getCompanyId(uid);
        this.detailService.distCarByPlateNum(oid, type, value, saId);
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        map.put("msg", "车辆分配成功！");
        return map;
    }

    @RequestMapping("/distCars")
    @ResponseBody
    public Map distCars(@RequestParam(name = "uid", defaultValue = "") String uid,
                        @RequestParam(name = "token", defaultValue = "") String token,
                        @RequestParam(name = "saId", defaultValue = "") String saId,
                        @RequestBody List<Map> data) throws Exception {
        String oid = TokenUtil.getCompanyId(uid);
        this.detailService.distCarsByPlateNum(oid, saId, data);
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        map.put("msg", "车辆分配成功！");
        return map;
    }


}
